/*
** 函数节流（时间差）：如果interval不传，则默认300ms
*/
export function throttle(fn: Function, interval?: number) {
  let enterTime = 0;
  let gapTime = interval || 300 ;
  return function() {
    let context = this;
    let backTime = new Date().getTime();
    if (backTime - enterTime > gapTime) {
      fn.call(context,arguments);
      enterTime = backTime;
    }
  };
}

/*
** 函数防抖（定时器）：如果interval不传，则默认300ms
*/
export function debounce(fn: Function, interval?: number) {
  let timer: any;
  let gapTime = interval || 300;
  return function() {
    clearTimeout(timer);
    let context = this;
    let args = arguments;
    timer = setTimeout(function() {
      fn.call(context, ...args, context);
    }, gapTime);
  };
}